<?php
namespace app\admin\controller\contents;
use app\common\controller\Backend;



class Kehu extends Backend
{

    protected $relationSearch = false;
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Kehu');
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $k => $v) {
                $v['vip'] = $this->model->tpList($v['vip']);
                $v['sex'] = $this->model->sexList($v['sex']);
                $v['truename']=strlen($v['truename'])>1 ? $v['truename'] : '-';
                $v['nickname']=strlen($v['nickname'])>1 ? $v['nickname'] : '-';
                $v['beizhu']=strlen($v['beizhu'])>1 ? $v['beizhu'] : '-';
                $v['atime'] = date("Y-m-d H:i:s",$v['atime']);
                $v['mobile'] = strlen($v['mobile'])<3 ? '未绑定' : $v['mobile'];
            }
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

    public function edit($ids=NULL){
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a");
            $params = array_filter(array_intersect_key(
                $params,
                array_flip(array('truename','mobile','cardno','beizhu','yg_id','num_fang','num_ting','house_state','house_type','mianji'))
            ));
            if(empty($params['truename']) || strlen($params['truename'])<1){
                $this->error('请输入员工姓名');
            }
            $params['sex']=(int)$_POST['row']['sex'];
            $params['vip']=(int)post('vip');
            if ($params) {
                Model('kehu')->where(array('id'=>$ids))->update($params);
                $this->success();
            }
            $this->error();
        }
        $this->view->assign('house_state',Model('zidian')->where(array('cid'=>3))->select());
        $this->view->assign('house_type',Model('zidian')->where(array('cid'=>1))->select());
        $this->view->assign('yg_list',Model('yuangong')->select());
        $this->view->assign('row',Model('kehu')->where(array('id'=>$ids))->find());
        return $this->view->fetch();
    }
    public function add(){
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a");
            $params = array_filter(array_intersect_key(
                $params,
                array_flip(array('truename','mobile','cardno','beizhu','yg_id','num_fang','num_ting','house_state','house_type','mianji'))
            ));
            if(empty($params['truename']) || strlen($params['truename'])<1){
                $this->error('请输入员工姓名');
            }
            $params['sex']=(int)$_POST['row']['sex'];
            $params['vip']=1;
            $params['username']=$params['mobile'];
            $params['atime']=TIMESTAMP;
            if ($params) {
                Model('kehu')->insert($params);
                $this->success();
            }
            $this->error();
        }
        $this->view->assign('house_state',Model('zidian')->where(array('cid'=>3))->select());
        $this->view->assign('house_type',Model('zidian')->where(array('cid'=>1))->select());
        $this->view->assign('yg_list',Model('yuangong')->select());
        return $this->view->fetch();
    }
    public function info($ids=NULL){
        $zidian=base_list();
        $info=Model('kehu')->where(array('id'=>$ids))->find();
        $info['house_state']=$info['house_state']>0 ? $zidian[$info['house_state']] : '-';
        $info['house_type']=$info['house_type']>0 ? $zidian[$info['house_type']] : '-';
        $this->view->assign('yg_list',Model('yuangong')->select());
        $this->view->assign('row',$info);
        return $this->view->fetch();
    }


}
